package com.itcaius.myandroidhomework.ui;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.itcaius.myandroidhomework.R;
import com.itcaius.myandroidhomework.adapter.BookAdapter;
import com.itcaius.myandroidhomework.data.model.Book;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caius on 2025/03/28.
 * BookListActivity
 */
public class BookListActivity extends AppCompatActivity {

    private ListView listView;
    private BookAdapter bookAdapter;
    private List<Book> bookList;
    private Button submitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booklits);

        listView = findViewById(R.id.book_list_view);

        //添加图书资源(实例)
        bookList = new ArrayList<>();
        bookList.add(new Book(R.drawable.book1, getString(R.string.micro_services),getString(R.string.class_time_1)));
        bookList.add(new Book(R.drawable.book2, getString(R.string.micro_services_in_action),getString(R.string.class_time_3)));
        bookList.add(new Book(R.drawable.book3, getString(R.string.spring_alibaba_sentinel),getString(R.string.class_time_2)));
        bookList.add(new Book(R.drawable.book4, getString(R.string.database_system),getString(R.string.class_time_4)));
        bookList.add(new Book(R.drawable.book5, getString(R.string.introduction_to_algorithms),getString(R.string.class_time_2)));
        bookList.add(new Book(R.drawable.book6, getString(R.string.thinking_in_java),getString(R.string.class_time_1)));

        bookAdapter = new BookAdapter(this, bookList);
        listView.setAdapter(bookAdapter);

        submitButton = findViewById(R.id.book_list_submit_btn);
        submitButton.setOnClickListener(v->{
            //返回选择之后的图书列表
            List<Book> hasSelect = bookAdapter.getBookList();
            StringBuilder builder = new StringBuilder();
            for(Book book: hasSelect){
                if(book.isSelected()){
                    builder.append(book.getTitle()).append("\n");
                }
            }
            new AlertDialog.Builder(this)
                    .setTitle("你选择的图书有：\n")
                    .setMessage(builder.toString())
                    .setPositiveButton("确定", null)
                    .show();
        });
    }
}
